import express = require('express')
import { AppDataSource } from "../../data-source"
import { User } from "../../entity/User";
import { Profile } from "../../entity/Profile";
import { expressjwt } from 'express-jwt'

const router = express.Router()
const ProfileRepository = AppDataSource.getRepository(Profile)

// $route  POST api/profiles/add
// @desc   创建信息接口
// @access private
router.post('/add', expressjwt({ secret: 'secret', algorithms: ['HS256'] }), async (req, res) => {
    const { companyName, myTime, careerName, link, isWriteTest, isFaceTest, writeTestTime, faceTestTime, source, remark } = req.body
    const profile = new Profile()
    Object.assign(profile, { companyName, myTime, careerName, link, isWriteTest, isFaceTest, writeTestTime, faceTestTime, source, remark })

    await AppDataSource.manager.save(profile)
    .then(profile => {
        res.json(profile)
    })
})

// $route  GET api/profiles
// @desc   获取所有信息
// @access private
router.get('/', expressjwt({ secret: 'secret', algorithms: ['HS256'] }), async (req, res) => {
    await ProfileRepository.find()
    .then(profile => {
        if (!profile) {
            res.status(404).json({
                msg: '没有任何内容！'
            })
        } else {
            res.json(profile)
        }
    })
    .catch(err => res.status(404).json(err))
})

// $route  GET api/profiles/:id
// @desc   获取单个信息
// @access private
router.get('/:id', expressjwt({ secret: 'secret', algorithms: ['HS256'] }), async (req, res) => {
    await ProfileRepository.findOneBy({
        id: req.params.id // 链接里的 在 req.params 中
    })
    .then(profile => {
        if (!profile) {
            res.status(404).json({
                msg: '没有任何内容！'
            })
        } else {
            res.json(profile)
        }
    })
})

// $route  post api/profiles/edit/:id
// @desc   编辑信息接口
// @access private
router.post('/edit/:id', expressjwt({ secret: 'secret', algorithms: ['HS256'] }), async (req, res) => {
    const { companyName, myTime, careerName, link, isWriteTest, isFaceTest, writeTestTime, faceTestTime, source, remark } = req.body
    const newProfile = { companyName, myTime, link, careerName, isWriteTest, isFaceTest, writeTestTime, faceTestTime, source, remark }

    const profile = await ProfileRepository.findOneBy({
        id: req.params.id
    })
    Object.assign(profile, newProfile)
    await ProfileRepository.save(profile)
    .then(() => {
        res.json(profile)
    })
    .catch(err => console.log(err))
})

// $route  POST api/profiles/edit/:id
// @desc   删除信息接口
// @access private
router.delete('/delete/:id', expressjwt({ secret: 'secret', algorithms: ['HS256'] }), async (req, res) => {
    const profile = await ProfileRepository.findOneBy({
        id: req.params.id
    })
    await ProfileRepository.remove(profile)
    .then(() => {
        res.json({
            msg: '已成功删除！'
        })
    })
    .catch(err => console.log(err))
})

module.exports = router
